// pages/courses/courses.js
wx.cloud.init();
const db = wx.cloud.database();
var util = require('../../util/util');
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    page: 1,  // 请求第几页的数据
    pageSize: 10, // 一页多少条数据
    total:0,
    first:false,
    all:true,
    agr:[],
    agreeshow:true,
    showView: false,
    showViews: false,
    sortway:'ms_time',
    sort_way:'时间',
    tag_s:'全部',
    content1_datas:[],
    content_datas:[],
    userInfo:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let user=wx.getStorageSync('user')
    console.log("进入小程序的index页面获取缓存",user)
    this.setData({
      userInfo:user,
    })
  },

  // 点击more出现排序页面
  click_more1:function(){
    this.setData({
      showView: (!this.data.showView),
      showViews:false
    })
  },
  click_more2:function(){
    this.setData({
      showViews: (!this.data.showViews),
      showView:false
    })
  },
  
  link:function(e)
  { 
    for(var i = 0; i < this.data.content_datas.length; i++)
    {
        if(this.data.content_datas[i]._id == e.currentTarget.id)
        {
          app.globalData.change = this.data.content_datas[i].change;
        } 
    }
    app.globalData.index = e.currentTarget.id;
    db.collection('forum').where({
      _id:e.currentTarget.id
      }).update({
      data:{
        browse:db.command.inc(1)
      },
    })
  },
  agree:function(e){
    for(var i=0;i<this.data.content_datas.length;i++)
    {
      if(e.currentTarget.id == this.data.content_datas[i]._id)
      { 
        var k = this.data.content_datas[i].hot+1
        this.setData({
          ['content_datas['+i+'].change']:true,
          ['content1_datas['+i+'].change']:true,
          ['content_datas['+i+'].hot']:k,
          ['content1_datas['+i+'].hot']:k
        })
      }
    }  
    let that = this
    this.setData({
      agreeshow:false
    })
    db.collection('forum').where({
      _id:e.currentTarget.id
      }).update({
      data:{
        hot:db.command.inc(1),
        agree:db.command.push(that.data.userInfo.nickName),
        change:true
      },
    })
  },
  agree_concel:function(e){
    for(var i=0;i<this.data.content_datas.length;i++)
    {
      if(e.currentTarget.id == this.data.content_datas[i]._id)
      {
        var k = this.data.content_datas[i].hot-1
        this.setData({
          ['content_datas['+i+'].change']:false,
          ['content1_datas['+i+'].change']:false,
          ['content_datas['+i+'].hot']:k,
          ['content1_datas['+i+'].hot']:k
        })
      }
    }  
    let that = this
    this.setData({
      agreeshow:true
    })
    db.collection('forum').where({
      _id:e.currentTarget.id
      }).update({
      data:{
        agree:db.command.pull(that.data.userInfo.nickName),
        hot:db.command.inc(-1),
        change:false
      },
    })
  },


  Search_Item:function (e) {
    let title = e.detail.value
    this.setData({
    page: 1,
    content_datas:[],
    total: 0,
    first:true
    })
    let that = this
    if(this.data.all){
    db.collection('forum')
    .where({
      title:db.RegExp({
        regexp: '.*' + title + '.*',//做为关键字进行匹配
        options: 'i',//不区分大小写
      }),
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data]
        })
        
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
         
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
       
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      
      }
    })
    db.collection('forum').get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  }
  else{
    
    db.collection('forum')
    .where({
          tag:this.data.tag_s,
          title:db.RegExp({
          regexp: '.*' + title + '.*',//做为关键字进行匹配
          options: 'i',//不区分大小写
           }),

    })
    .orderBy(that.data.sortway,'desc')
    .get({
      success: function(res) {
        that.setData({
          content_datas:res.data
        })
        
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
         
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
       
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      
      }
    })
    db.collection('forum').get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  }
  },
  //排序函数
  compare: function (property) {
    return function (a, b) {
      var value1 = a[property]
      var value2 = b[property]
      return value2 - value1
    }
  },
  //选择按发布时间排序
  sort_by_time:function (e) {
    /*
    console.log(this.data.content1_datas)
    this.data.content1_datas.sort(this.compare("ms_time"))
    console.log(123)
    console.log(this.data.content_datas)
    this.setData({
      showView: (!this.data.showView),
      content_datas:(this.data.content1_datas),
      sortway :'ms_time',
      sort_way:'时间'
    })
    console.log(this.data.content1_datas)*/
    this.setData({
      showView: (!this.data.showView),
      sortway :'ms_time',
      sort_way:'时间',
      page: 1,
      content_datas:[],
      total: 0,
      first:true
    })
    let that = this
    console.log(this.data.all)
    if(this.data.tag_s == '全部')
    {
      db.collection('forum')
    .orderBy('ms_time','desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data]
        })
        
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
         
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
       
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      
      }
    })
    db.collection('forum').get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
    }
    else{
      db.collection('forum')
    .where({
      tag:this.data.tag_s
    })
    .orderBy('ms_time','desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data]
        })
        
        for (var i = that.data.page * that.data.pageSize; i < that.data.content_datas.length; i++) {
          
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
       
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      
      }
    })
    db.collection('forum').get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
    }
    
  },
  //选择按热度排序
  sort_by_hot:function (e) {
   /* this.data.content1_datas.sort(this.compare("hot"))
    this.setData({
      showView: (!this.data.showView),
      content_datas:(this.data.content1_datas),
      sortway :'hot',
      sort_way:'热度',
      first:true
    })
    console.log(this.data.content1_datas)*/
    this.setData({
      showView: (!this.data.showView),
      sortway :'hot',
      sort_way:'热度',
      page: 1,
      content_datas:[],
      total: 0,
      first:true
    })
    let that = this

    if(this.data.tag_s=="全部")
    {
      db.collection('forum')
      .orderBy('hot','desc')
      .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
      .limit(this.data.pageSize)  //每个页面限制数据数
      .get({
        success: function(res) {
          that.setData({
            content_datas:[...that.data.content_datas,...res.data]
          })
          
          for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
         
            if(that.data.content_datas[i].content.length>=75){
              that.setData({
                ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
              })
            }
            else{
              that.setData({
                ['content_datas['+i+'].content1']:that.data.content_datas[i].content
              })
            }
          }
         
          for(var i = 0; i < that.data.content_datas.length; i++)
          {
            that.setData({
              ['content_datas['+i+'].change']:false
            })
            for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
            { 
              if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
              {
                that.setData({
                  ['content_datas['+i+'].change']:true
                })
              }
            }  
          }
          
          
          that.setData({
            'content1_datas' :that.data.content_datas
          })
        
        }
      })
      db.collection('forum').get({
        success: (res) => {
          this.setData({
            total: res.data.length  //总共有多少条商品数据
          })
        }
      })
    }
    else{
      db.collection('forum')
      .where({
        tag:this.data.tag_s
      })
      .orderBy('hot','desc')
      .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
      .limit(this.data.pageSize)  //每个页面限制数据数
      .get({
        success: function(res) {
          that.setData({
            content_datas:[...that.data.content_datas,...res.data]
          })
          
          for (var i = that.data.page * that.data.pageSize; i < that.data.content_datas.length; i++) {
           
            if(that.data.content_datas[i].content.length>=75){
              that.setData({
                ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
              })
            }
            else{
              that.setData({
                ['content_datas['+i+'].content1']:that.data.content_datas[i].content
              })
            }
          }
         
          for(var i = 0; i < that.data.content_datas.length; i++)
          {
            that.setData({
              ['content_datas['+i+'].change']:false
            })
            for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
            { 
              if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
              {
                that.setData({
                  ['content_datas['+i+'].change']:true
                })
              }
            }  
          }
          
          
          that.setData({
            'content1_datas' :that.data.content_datas
          })
        
        }
      })
      db.collection('forum').get({
        success: (res) => {
          this.setData({
            total: res.data.length  //总共有多少条商品数据
          })
        }
      })
    }
   
  },
  //筛选出不同tag的内容
  select_0:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      content_datas:[],
      total: 0,
      all:true,
      first:true
    })
    let that = this
    db.collection('forum')
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"全部"
        })
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
         
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })
    db.collection('forum')
    .get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  },
  select_1:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      content_datas:[],
      content1_datas:[],
      total: 0,
      all:false,
      first:true
    })
    let that = this
    db.collection('forum')
    .where({
      tag:"吐槽"
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"吐槽"
        })
        
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
        
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
         
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })
  
    db.collection('forum')
    .where({
      tag:"吐槽"
    }).get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  },
  select_2:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      all:false,
      content_datas:[],
      content1_datas:[],
      total: 0,
      first:true
    })
    let that = this
    db.collection('forum')
    .where({
      tag:"生活"
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"生活"
        })
  
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
          
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
         
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })
 
    db.collection('forum')
    .where({
      tag:"生活"
    }).get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  },
  select_3:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      all:false,
      content_datas:[],
      content1_datas:[],
      total: 0,
      first:true
    })
    let that = this
    db.collection('forum')
    .where({
      tag:"闲聊"
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"闲聊"
        })
      
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
        
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
         
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })

    db.collection('forum')
    .where({
      tag:"闲聊"
    }).get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  },
  select_4:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      all:false,
      content_datas:[],
      content1_datas:[],
      total: 0,
      first:true
    })
    let that = this
    db.collection('forum')
    .where({
      tag:"求助"
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"求助"
        })
        
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
          
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
         
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })
   
    db.collection('forum')
    .where({
      tag:"求助"
    }).get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  },
  select_5:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      all:false,
      content_datas:[],
      content1_datas:[],
      total: 0,
      first:true
    })
    let that = this
    db.collection('forum')
    .where({
      tag:"麻了"
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"麻了"
        })
       
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
       
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
         
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })
    
    db.collection('forum')
    .where({
      tag:"麻了"
    }).get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  },
  select_6:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      all:false,
      content_datas:[],
      content1_datas:[],
      total: 0,
      first:true
    })
    let that = this
    db.collection('forum')
    .where({
      tag:"考研"
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"考研"
        })
       
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
          
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
         
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })
    
    db.collection('forum')
    .where({
      tag:"考研"
    }).get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  },
  select_7:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      all:false,
      content_datas:[],
      content1_datas:[],
      total: 0,
      first:true
    })
    let that = this
    db.collection('forum')
    .where({
      tag:"保研"
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"保研"
        })
       
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
         
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
         
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })

    db.collection('forum')
    .where({
      tag:"保研"
    }).get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  },
  select_8:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      all:false,
      content_datas:[],
      content1_datas:[],
      total: 0,
      first:true
    })
    let that = this
    db.collection('forum')
    .where({
      tag:"考公"
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"考公"
        })
        
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
          
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
         
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })
   
    db.collection('forum')
    .where({
      tag:"考公"
    }).get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  }, 
  
  select_9:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      all:false,
      content_datas:[],
      content1_datas:[],
      total: 0,
      first:true
    })
    let that = this
    db.collection('forum')
    .where({
      tag:"就业"
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"就业"
        })
       
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
         
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
         
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })
    
    db.collection('forum')
    .where({
      tag:"就业"
    }).get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  }, 
  select_10:function () {
    this.setData({
      showViews: (!this.data.showViews),
      page: 1,
      all:false,
      content_datas:[],
      content1_datas:[],
      total: 0,
      first:true
    })
    let that = this
    db.collection('forum')
    .where({
      tag:"其他"
    })
    .orderBy(that.data.sortway,'desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data],
          tag_s:"其他"
        })
        
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
          
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
         
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      }
    })
   
    db.collection('forum')
    .where({
      tag:"其他"
    }).get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
  }, 

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      page: 1,
      all:false,
      content_datas:[],
      content1_datas:[],
      total: 0,
    })
    let that = this
    if(this.data.tag_s == '全部')
    {
      db.collection('forum')
    .orderBy('ms_time','desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data]
        })
        console.log(132,that.data.content_datas)
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
         
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
       
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      
      }
    })
    console.log(132,that.data.content_datas)
    db.collection('forum').get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
    }
    else
    {
      db.collection('forum')
      .where({
        tag:that.data.tag_s
      })
    .orderBy('ms_time','desc')
    .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
    .limit(this.data.pageSize)  //每个页面限制数据数
    .get({
      success: function(res) {
        that.setData({
          content_datas:[...that.data.content_datas,...res.data]
        })
        console.log(132,that.data.content_datas)
        for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
         
          if(that.data.content_datas[i].content.length>=75){
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
            })
          }
          else{
            that.setData({
              ['content_datas['+i+'].content1']:that.data.content_datas[i].content
            })
          }
        }
       
        for(var i = 0; i < that.data.content_datas.length; i++)
        {
          that.setData({
            ['content_datas['+i+'].change']:false
          })
          for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
            {
              that.setData({
                ['content_datas['+i+'].change']:true
              })
            }
          }  
        }
        
        
        that.setData({
          'content1_datas' :that.data.content_datas
        })
      
      }
    })
    console.log(132,that.data.content_datas)
    db.collection('forum').get({
      success: (res) => {
        this.setData({
          total: res.data.length  //总共有多少条商品数据
        })
      }
    })
    }
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 1,
      content_datas:[],
      total: 0,
      tag_s:'全部',
      sort_way:'时间',
    })
    this.onShow()
    wx.showToast({
      icon:'success',
      title: '刷新成功！'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.page * this.data.pageSize >= this.data.total){
      //没有下一页数据了
      return wx.showToast({
        title: '没有更多帖子了捏!',
        icon: 'none'
      })
  }


  // 页码值加一
  this.setData({
      page: this.data.page +1
  })

  
  wx.showToast({  // 显示加载中loading效果2秒
    title: '加载中',
    icon: 'loading',
  });
  let that = this
  if(!this.data.first)
  {
    this.onShow()
  }
  else{
     if(that.data.tag_s == '全部')
  {
  db.collection('forum')
  .orderBy(that.data.sortway,'desc')
  .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
  .limit(this.data.pageSize)  //每个页面限制数据数
  .get({
    success: function(res) {
      that.setData({
        content_datas:[...that.data.content_datas,...res.data],
      })

      for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
    
        if(that.data.content_datas[i].content.length>=75){
          that.setData({
            ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
          })
        }
        else{
          that.setData({
            ['content_datas['+i+'].content1']:that.data.content_datas[i].content
          })
        }
      }
       
      for(var i = 0; i < that.data.content_datas.length; i++)
      {
        that.setData({
          ['content_datas['+i+'].change']:false
        })
        for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
        { 
          if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
          {
            that.setData({
              ['content_datas['+i+'].change']:true
            })
          }
        }  
      }
      
      that.setData({
        'content1_datas' :that.data.content_datas
      })
    }
  })
  }
  else{
  db.collection('forum')
  .where({
    tag:that.data.tag_s
  })
  .orderBy(that.data.sortway,'desc')
  .skip((this.data.page-1)*this.data.pageSize) //跳过已加载页面的数据
  .limit(this.data.pageSize)  //每个页面限制数据数
  .get({
    success: function(res) {
      that.setData({
        content_datas:[...that.data.content_datas,...res.data],
      })
     
      for (var i = (that.data.page-1) * that.data.pageSize; i < that.data.content_datas.length; i++) {
       
        if(that.data.content_datas[i].content.length>=75){
          that.setData({
            ['content_datas['+i+'].content1']:that.data.content_datas[i].content.slice(0,75) + '......'
          })
        }
        else{
          that.setData({
            ['content_datas['+i+'].content1']:that.data.content_datas[i].content
          })
        }
      }
       
      for(var i = 0; i < that.data.content_datas.length; i++)
      {
        that.setData({
          ['content_datas['+i+'].change']:false
        })
        for(var j = 0; j < that.data.content_datas[i].agree.length; j++)
        { 
          if(that.data.userInfo.nickName == that.data.content_datas[i].agree[j])
          {
            that.setData({
              ['content_datas['+i+'].change']:true
            })
          }
        }  
      }
      
      that.setData({
        'content1_datas' :that.data.content_datas
      })
    }
  })
  }
  }
 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})